home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Dev / misc / temgen.lha / Temgen / tg-0.11 / sample / getopt / opdef.tg < prev    next >
Text File  |  2002-12-18  |  2KB  |  84 lines

  1. #
  2. #  definitions for 'options.tg' example
  3. #
  4. @function init()
  5. @  $options_defined = 0
  6. @  $incl = 0
  7. @  $optstring = ""
  8. @  $types = [ i : "int", s : "char *" ]
  9. @endfunction
  10.  
  11. @function include( name )
  12. @if ( $incl.$name != 1 )
  13. @   $incl.$name = 1
  14. @   emit "include"
  15. \#line $tplline() \"$tplfile()\"
  16. \#include $name
  17. @endif    
  18. @endfunction    
  19.  
  20. @function option( letter, type, name, default )
  21. @   if ( !$options_defined )
  22. @       $options_defined = 1
  23. @       $include( "<unistd.h>" )
  24. @       emit "options"
  25. \#line $tplline() \"$tplfile()\"
  26. \
  27. /* runtime options */
  28. \
  29.  
  30. @   endif   
  31.  
  32. @   emit "options"
  33. \#line $tplline() \"$tplfile()\"
  34. $types.$type       $name \= $default ;
  35. @   $optstring = $optstring + $letter
  36. @   if  ( $type == 's' )
  37. @       $optstring = $optstring + ':'
  38. @   endif
  39.  
  40. @   emit "case"
  41.             case \'$letter' :
  42. @           switch $type
  43. @                case 'i':
  44.                    $name \= 1;
  45. @                  break
  46. @                case 's':
  47.                    $name \= optarg;
  48. @                  break
  49. @           endswitch
  50. @endfunction    
  51.  
  52. @function  main()
  53. #
  54. #  program body:
  55. #
  56. @output( "stdout" )    
  57. @embed "include"
  58. @embed "options"
  59. \#line $tplline() \"$tplfile()\"
  60.  
  61. void parse_options( int argc, char *argv[] )
  62. {
  63.     int opt = 0;
  64.     
  65.     while( opt >= 0 ) {
  66.         opt = getopt( argc, argv, \"$optstring" );
  67.         switch( opt ) {
  68. @embed "case"
  69. \#line $tplline() \"$tplfile()\"
  70.             default:
  71.                 break;
  72.         }
  73.     }
  74.     
  75. }    
  76.  
  77. int main( int argc, char *argv[] )
  78. {
  79.     parse_options( argc, argv );
  80.     return 0;
  81. }
  82.  
  83. @endfunction
  84.